package Reseau;

import Controler.Controler;
import protocol.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class Send {


    private int portDest = 2235;
    private Controler control;

    public Send(Controler p_control){
        this.control = p_control;
    }

    public void sendMessage(InetAddress addressDest, Message m){
        try {
            //création du socket
            DatagramSocket monSocket = new DatagramSocket();
            //serialisation: mise en byte du message pour pouvoir être envoyé
            byte[] buf = control.getChatni().serialiser(m);
            //envoi du message
            DatagramPacket packet = new DatagramPacket(buf, buf.length, addressDest, portDest);
            System.out.println("Adresse dest: "+ addressDest);
            if(m instanceof ChatMessage) System.out.println("Message "+ ((ChatMessage)m).getMessage());
            System.out.println("J'envoie notre paquet " + m);
            monSocket.send(packet);
            System.out.println("Le paquet est envoyé");
        }
        catch (Exception ex) {
            System.out.println("Probleme: classe Send méthode run");;
        }
    }

}
